#ifndef _ANIMATION_CONTROLLER_H_
#define _ANIMATION_CONTROLLER_H_

#include "AskalExport.h"
#include "hltypes/hstring.h"
#include "hltypes/harray.h"
#include "hltypes/hmap.h"
#include "Skeleton.h"
#include "AprilMesh.h"
#include "AskalDataCache.h"

namespace askal
{
	struct Track
	{
		hstr mAnimation;
		float mTime;
		float mSpeed;
		bool mPaused;
		int mLoop;
		float mWeight;
		
		int __lastIndex; // used for optimised keyframe search;
		
		harray<askal::Bone> mSnapshot; // bone snapshot used for track animation storage needed for blending with different tracks
	};

	class AskalExport AnimationController
	{
		hmap<hstr, Track> _mTracks;
		
		hstr _mSkeletonName;
		hstr _mMeshName;
		
		float EPSILON;
		
		void _updateBones(Bone* root, Skeleton *skeleton);
		int _findKeys(chstr bone, Track *track, Keyframe* k1, Keyframe* k2, float *t);
		
	public:
		AnimationController();
		~AnimationController();
		
		void assignSkeleton(hstr skeletonName);
		void assignMesh(hstr meshName);
		
		void playAnimation(chstr animationName);
		void stopAnimation(chstr animationName);
		void stopAllAnimations();
		void loopAnimation(chstr animationName, int period = -1);
		void pauseAllAnimations();
		void pauseAnimation(chstr animationName);
		void resumeAnimation(chstr animationName);
		void resumeAllAnimations();
		void setAnimationSpeed(chstr animationName, float speed);
		
		void update(float dt);
		
		void updateSkeletonVertexBatch(harray<April::PlainVertex> *skeletonVertices);
		void updateMeshVertexBatch(harray<April::PlainVertex> *meshVertices);

	};

}

#endif // _ANIMATION_CONTROLLER_H_
